<template>
    <el-dialog v-model="visible" title="加入回访任务" :close-on-click-modal="false" :close-on-press-escape="false"
      style="width: 30%;">
      <el-form :model="state.dataForm" ref="dataFormRef" label-width="85px" @keyup.enter="state.getDataList()" v-loading="state.dataForm.loading">
        <el-form-item label="回访任务">
          <el-select v-model="state.dataForm.tskId" placeholder="请选择" style="width: 60%;" clearable>
            <el-option v-for="role in roleList" :key="role.id" :label="role.tskName" :value="role.id"></el-option>
          </el-select>
          <!-- <el-checkbox v-model="state.dataForm.contacted" style="margin-left: 18px;">已联络的也移除</el-checkbox> -->
        </el-form-item>
      </el-form>
      <template v-slot:footer>
        <div style="display: flex; justify-content: center;">
          <el-button type="primary" @click="getadd()">加入</el-button>
          <!-- <el-button type="danger" @click="gatdelete()">移除</el-button> -->
          <el-button @click="close()">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </template>

  <script lang="ts" setup>
  import useView from "@/hooks/useView";
  import { reactive, ref, toRefs, } from "vue";
  import baseService from "@/service/baseService";
  import { ElMessage } from "element-plus";
  const emit = defineEmits(["refreshDataList"]);

  const visible = ref(false);
  const dataFormRef = ref();
  const view = reactive({
    dataForm: {
      loading: false,
      id: "",
      tskId: "",
      name: "",
      checkAll: false as any,
      condition: "",
      contacted: false,

    },
    dataForm2: {
      psnIdList: [] as any[],
    },

  });

  const init = (id: any, data: any) => {
    visible.value = true;
    state.dataForm = data;
    state.dataForm.id = '';
    state.dataForm.checkAll = data.checkAll;
    state.dataForm2.psnIdList = data.psnIdList.split(',').map((x: string) => parseInt(x))
    // 重置表单数据
    if (dataFormRef.value) {
      dataFormRef.value.resetFields();
    }
    getTasklist()
  };


  //任务列表
  const roleList = ref<any[]>([]);
  const getTasklist = () => {
    return baseService.get("noc/nocrepaytask/page", { page: 1, limit: 500}).then((res) => {
      roleList.value = res.data.list;
    });
  };


  //关闭
  const close = () => {
    visible.value = false;
    state.dataForm.tskId = "";
  }


  //加入任务
  const getadd = () => {
    if (!state.dataForm.tskId) {
      ElMessage.warning('请选择联络任务');
      return;
    }
    state.dataForm.loading = true;
    state.dataForm2.psnIdList = state.dataForm2.psnIdList
    baseService.post("/noc/nocrepay/addRepay", {
      tskId: Number(state.dataForm.tskId),
      psnIdList: state.dataForm2.psnIdList,
      checkAll: state.dataForm.checkAll,
      condition: state.dataForm
    }
    ).then((res) => {
      if (res.code == 0) {
        // 重新获取数据列表
        state.dataForm2.psnIdList = [];
        state.dataForm.tskId = "";
        state.dataForm.checkAll = false;
        visible.value = false;
        state.dataForm.loading = false;
        state.multiple = !state.dataForm.checkAll;
        emit("refreshDataList");
        return ElMessage.success({
          message: "已将"+res.data.insertCount+"条数据加入到任务中",
          type: "success",
          duration: 5000,
        });
      }
    });
  }

  const state = reactive({ ...useView(view), ...toRefs(view) });

  defineExpose({
    init
  });
  </script>
